<template>
  <el-scrollbar wrap-style="border-radius: 10px !important;" :style="{ 'max-height': courseheight }" class="coursebox">
    <div class="innerbox" :style="{ 'min-height': courseheight }">
      <div class="clearfix m-t-20">
        <div class="searchbox" :class="{ borderblue: showblue }">
          <input @focus="showblue = true" @blur="showblue = false" type="text" id="searchinp" placeholder="搜索"
            v-model="keywords" autocomplete="off" />
          <el-icon @click="clearbox" size="18" class="cursorbtn" color="#e1e1e5" style="margin-right: 3px">
            <CircleCloseFilled v-show="showclear" />
          </el-icon>
          <el-icon size="25" class="cursorbtn colorChange" :class="{ colorblue: showblue }" color="#e1e1e5">
            <Search />
          </el-icon>
        </div>
      </div>
      <div class="m-t-30">
        <ul class="courselistul clearfix">
          <li class="courseitem" v-for="(item, index) in lessonlist" :key="index">
            <div class="imgbox" @click="gotoCourseIndex(item.id)">
              <img src="@/assets/images/index/course1.jpg" :alt="item.title" />
            </div>
            <div class="infobox">
              <h3 class="coursename">{{ item.title }}</h3>
              <!-- <p class="schoolname m-t-10">加里顿大学</p> -->
              <p class="teachername">任课老师：{{ item.teacher_name }}</p>
              <!-- <p class="duringtime">开课时间：2021-12-22～2023-12-22</p> -->
              <p class="duringtime">开课时间：{{ item.start_time }}</p>
            </div>
          </li>
          <li class="courseitem">
            <div class="imgbox">
              <img src="@/assets/images/index/course2.jpg" alt="课程一" />
            </div>
            <div class="infobox">
              <h3 class="coursename">电子信息工程专业科技写作</h3>
              <!-- <p class="schoolname m-t-10">加里顿大学</p> -->
              <p class="teachername">因雀斯汀·纽皮</p>
              <p class="duringtime">开课时间：2021-12-22～2023-12-22</p>
            </div>
          </li>
          <li class="courseitem">
            <div class="imgbox">
              <img src="@/assets/images/index/course3.jpg" alt="课程一" />
            </div>
            <div class="infobox">
              <h3 class="coursename">电子系统创新方法课程设计</h3>
              <!-- <p class="schoolname m-t-10">加里顿大学</p> -->
              <p class="teachername">因雀斯汀·纽皮</p>
              <p class="duringtime">开课时间：2021-12-22～2023-12-22</p>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </el-scrollbar>
</template>

<script setup>
import { ref, onMounted, watch, inject } from 'vue'
import { useRouter } from 'vue-router'
import api from '@/request/api'

// 加载user模块 以及底下变量
import { storeToRefs } from 'pinia'
import useStore from '@/store'
const { user } = useStore()
let { username, avatar, uid } = storeToRefs(user)

// 初始化路由实例
const router = useRouter()
// 注入网络请求方法
const axios = inject('axios')

let courseheight = ref(0)
let keywords = ref('')
let showclear = ref(false)
let showblue = ref(false)
let lessonlist = ref([])

onMounted(async () => {
  // 设置课程中间视图的最小高度
  courseheight.value = `${window.innerHeight - 60}px`

  // 获取课程
  getmylesson()
})

// 获取课程
async function getmylesson() {
  let res = await axios.post(api.lessonmylist, {
    uid: uid.value
  })

  if (res.code == 0) {
    // console.log('lessonmylist', res)
    lessonlist.value = res.data
  } else {
    showMessage('error', res.msg)
  }
}
// 非空字符串时显示×号图标
watch(keywords, (newval, oldval) => {
  newval.length > 0 ? (showclear.value = true) : (showclear.value = false)
})

// 点击×号 清空搜索框
function clearbox() {
  keywords.value = ''
  // 因为点击其他元素 input需要重新获取焦点
  document.querySelector('#searchinp').focus()
}

// 课程主页跳转
function gotoCourseIndex(lessonId) {
  // 新页面打开
  // const hrefstr = router.resolve({
  //   name: 'CourseIndex',
  //   state: {
  //     id: 110,
  //     oo: 666
  //   }
  // }).href
  // window.open(hrefstr, '_blank')
  console.log('lessonId', lessonId);

  router.push({
    name: 'CourseIndex',
    state: {
      lessonId: lessonId,
    }
  })
}

// 信息提示
const showMessage = (msgtype, msgstr) => {
  ElMessage({
    showClose: true,
    message: msgstr,
    type: msgtype
  })
}
</script>

<style scoped>
.coursebox {
  background-color: #f2f4f7;
  padding: 30px;
  /* padding-bottom: 0; */
  /* padding-right: 0; */
}

.innerbox {
  background-color: #fff;
  border-radius: 10px;
  padding: 10px;
  overflow: hidden;
}

.scrollbar-demo-item {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  margin: 10px;
  text-align: center;
  border-radius: 4px;
  background: var(--el-color-primary-light-9);
  color: var(--el-color-primary);
}

.searchbox {
  float: right;
  display: flex;
  width: 200px;
  height: 36px;
  border: 1px solid #d4d6d9;
  font-size: 14px;
  color: #131b26;
  padding: 0 14px;
  border-radius: 40px;
  align-items: center;
  margin-right: 50px;
}

.searchbox input {
  width: 155px;
  height: 36px;
}

.searchbox:hover {
  border-color: #3a8bff;
}

.searchbox:hover .colorChange {
  color: #3a8bff !important;
}

.borderblue {
  border-color: #3a8bff;
}

.colorblue {
  color: #3a8bff !important;
}

.cursorbtn:hover {
  cursor: pointer;
}

.searchbox:focus {
  /* border-color: #3a8bff; */
  background-color: #000;
}

#searchinp:focus .colorChange {
  color: #3a8bff !important;
}

/* 课程列表具体样式 */
.courselistul {
  width: 840px;
  margin: 0 auto;
}

.courseitem {
  float: left;
  position: relative;
  width: 240px;
  height: 260px;
  font-size: 12px;
  box-sizing: border-box;
  vertical-align: top;
  color: #a8a8b3;
  overflow: visible;
  cursor: pointer;
  margin-right: 40px;
  margin-bottom: 0;
}

.courseitem .imgbox {
  width: 240px;
  height: 130px;
  border-radius: 8px;
  position: relative;
  margin-bottom: 12px;
}

.courseitem .infobox {
  position: relative;
  box-sizing: border-box;
  padding: 0 6px;
}

.imgbox img {
  display: inline-block;
  width: 100%;
  height: 100%;
  border-radius: 8px;
}

.infobox h3 {
  display: inline-block;
  width: 100%;
  position: relative;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
}

.infobox p {
  line-height: 17px;
  margin-top: 8px;
  font-size: 12px;

  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  line-clamp: 1;
  -webkit-box-orient: vertical;
}

.infobox .coursename {
  color: #131b26;
  margin: 0;
}

.infobox .schoolname {
  color: #474c59;
}

.infobox .teachername {
  color: #8a8b99;
  height: 17px;
}
</style>
